package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

/**
 * Provides test cases.
 * 
 * @author Ed Kim
 * @author Jon Lao
 * @author JianFei Liao
 * @author Jared Sunouchi
 */
public class TestOrganizationActionBean {

  /** Get the test host. */
  private String testHost = System.getProperty("test.host");
  /** A string representing the homepage title */
  private static final String HOMEPAGE_TITLE = "MyISERN 2.0 - Home";
  /** A string representing the login page title */
  private static final String LOGIN_TITLE = "MyISERN 2.0 - Login";
  /** A string represent the username attribute */
  private static final String USERNAME_FIELD = "username";
  /** A string represent the password attribute */
  private static final String PASSWORD_FIELD = "password";
  /** A string representing the user name */
  private static final String USERNAME = "username";
  /** A string representing the password */
  private static final String PASSWORD = "password";
  /** A string representing the collaboration form */
  private static final String ORGANIZATION_FORM = "OrganizationForm";
  /** A string to represent the name field */
  private static final String NAME = "name";
  
  /**
   * Tests the organization function for adding under normal situations.
   *
   * @throws Exception If problems occur.
   */
  @Test
  public void testAddOrganizationActionBean() throws Exception {
    WebConversation conversation = new WebConversation();
    
    // retrieves the index.jsp page and check the title
    String url = this.testHost + "myisern";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Clicking link without login", LOGIN_TITLE, response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(USERNAME_FIELD, USERNAME);
    loginRequest.setParameter(PASSWORD_FIELD, PASSWORD);
    response = conversation.getResponse(loginRequest);
    assertEquals("Logged in", HOMEPAGE_TITLE, response.getTitle());
    
    WebLink link = response.getLinkWithID("addOrganization");
    response = link.click();
    assertEquals("Adding organization", "MyISERN 2.0 - Add Organization", response.getTitle());
    
    WebForm addOrganizationForm = response.getFormWithID(ORGANIZATION_FORM);
    WebRequest addOrganizationRequest = addOrganizationForm.getRequest("reset");
    addOrganizationRequest.setParameter(NAME, "Testing Clear");
    response = conversation.getResponse(addOrganizationRequest);
    
    addOrganizationForm = response.getFormWithID(ORGANIZATION_FORM);
    addOrganizationRequest = addOrganizationForm.getRequest("add");
    response = conversation.getResponse(addOrganizationRequest);
    
    addOrganizationForm = response.getFormWithID(ORGANIZATION_FORM);
    addOrganizationRequest = addOrganizationForm.getRequest("add");
    
    addOrganizationRequest.setParameter(NAME, "Test Name");
    response = conversation.getResource(addOrganizationRequest);
    
    addOrganizationRequest.setParameter("type", "Test Type");
    addOrganizationRequest.setParameter("contact", "Test Contact");
    addOrganizationRequest.setParameter("affiliatedResearchers", 
        "Test Researcher 1, Test Researcher 2");
    addOrganizationRequest.setParameter("country", "Test Country");
    addOrganizationRequest.setParameter("researchKeywords", "Test Keyword 1, Test Keyword 2");
    addOrganizationRequest.setParameter("researchDescription", "Test Research Description");
    addOrganizationRequest.setParameter("homePage", "www.testing.com");
    
    response = conversation.getResponse(addOrganizationRequest);
    
    link = response.getLinkWithID("addOrganization");
    response = link.click();
    assertEquals("Adding organization", "MyISERN 2.0 - Add Organization", response.getTitle());
    
    addOrganizationForm = response.getFormWithID(ORGANIZATION_FORM);
    addOrganizationRequest = addOrganizationForm.getRequest("add");
    
    addOrganizationRequest.setParameter(NAME, "Test Name");
    addOrganizationRequest.setParameter("type", "Test Type");
    addOrganizationRequest.setParameter("contact", "Test Contact");
    addOrganizationRequest.setParameter("affiliatedResearchers", 
        "Test Researcher 1, Test Researcher 2");
    addOrganizationRequest.setParameter("country", "Test Country");
    addOrganizationRequest.setParameter("researchKeywords", "Test Keyword 1, Test Keyword 2");
    addOrganizationRequest.setParameter("researchDescription", "Test Research Description");
    addOrganizationRequest.setParameter("homePage", "www.testing.com");
    
    response = conversation.getResponse(addOrganizationRequest);
    assertEquals("Went to edit page", "MyISERN 2.0 - Edit Organization", response.getTitle());
    
    link = response.getLinkWithID("addOrganization");
    response = link.click();
    assertEquals("Adding oragnization", "MyISERN 2.0 - Add Organization", response.getTitle());
    
    addOrganizationForm = response.getFormWithID(ORGANIZATION_FORM);
    addOrganizationRequest = addOrganizationForm.getRequest("cancel");
    response = conversation.getResponse(addOrganizationRequest);
    
    link = response.getLinkWithID("logout");
    link.click();
    response = conversation.getCurrentPage();
    assertEquals("Logged out", LOGIN_TITLE, response.getTitle());
  }
  
  /**
   * Tests the organization function for editing under normal situations.
   *
   * @throws Exception If problems occur.
   */
  @Test
  public void testEditOrganizationActionBean() throws Exception {
    WebConversation conversation = new WebConversation();
    
    // retrieves the index.jsp page and check the title
    String url = this.testHost + "myisern";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Clicking link without login", LOGIN_TITLE, response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(USERNAME_FIELD, USERNAME);
    loginRequest.setParameter(PASSWORD_FIELD, PASSWORD);
    response = conversation.getResponse(loginRequest);
    assertEquals("Logged in", HOMEPAGE_TITLE, response.getTitle());
    
    WebLink link = response.getLinkWithID("editOrganization");
    response = link.click();
    assertEquals("Checking edit organization page retrieval", "MyISERN 2.0 - Edit Organization",
        response.getTitle());
    
    WebForm editOrganizationForm = response.getFormWithID(ORGANIZATION_FORM);
    WebRequest editOrganizationRequest = editOrganizationForm.getRequest();
    
    editOrganizationRequest.setParameter(NAME, "Test Name");
    
    response = conversation.getResponse(editOrganizationRequest);
    
    editOrganizationForm = response.getFormWithID(ORGANIZATION_FORM);
    editOrganizationRequest = editOrganizationForm.getRequest("edit");
    
    editOrganizationRequest.setParameter("researchDescription", "Test Description Edited");
    
    response = conversation.getResponse(editOrganizationRequest);
    
    editOrganizationForm = response.getFormWithID(ORGANIZATION_FORM);
    editOrganizationRequest = editOrganizationForm.getRequest("remove");
    response = conversation.getResponse(editOrganizationRequest);
    
    link = response.getLinkWithID("logout");
    link.click();
    response = conversation.getCurrentPage();
    assertEquals("Logged out", LOGIN_TITLE, response.getTitle());
  }
}
